package com.gin.no_gay_alliance.utils;

import android.graphics.drawable.Drawable;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * Glide加载图像到TextView中的Target
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/31 11:56
 **/
public class TextViewTarget extends CustomViewTarget<TextView, Drawable> {
    /**
     * 图片加载到哪个位置 0~3
     */
    final int position;
    /**
     * 宽,如果不设置则使用图片原宽
     */
    final Integer width;
    /**
     * 高,如果不设置则使用图片原高
     */
    final Integer height;

    public TextViewTarget(@NonNull TextView view, int position) {
        this(view, position, null, null);
    }

    public TextViewTarget(@NonNull TextView view, int position, Integer width, Integer height) {
        super(view);
        this.position = position;
        this.width = width;
        this.height = height;
    }

    @Override
    protected void onResourceCleared(@Nullable Drawable placeholder) {
    }

    @Override
    public void onLoadFailed(@Nullable Drawable errorDrawable) {
        configDrawable(errorDrawable);
    }

    @Override
    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
        configDrawable(resource);
    }

    public void configDrawable(Drawable drawable) {
        if (drawable == null) {
            view.setCompoundDrawables(null, null, null, null);
            return;
        }
        drawable.setBounds(0, 0, width != null ? width : drawable.getMinimumWidth(), height != null ? height : drawable.getMinimumHeight());
        switch (position) {
            case 1:
                view.setCompoundDrawables(null, drawable, null, null);
                break;
            case 2:
                view.setCompoundDrawables(null, null, drawable, null);
                break;
            case 3:
                view.setCompoundDrawables(null, null, null, drawable);
                break;
            default:
                view.setCompoundDrawables(drawable, null, null, null);
        }
    }
}
